<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'代理等级管理'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	pdo_delete('jy_area_psi_level',array('shoplevelid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$default_level=$this->plugin_setting('default_level');
	$list=array(
			array(
				'id'=>'a0',
				'title'=>'默认等级',
				'level'=>0,
			),
			array(
				'id'=>'a1',
				'title'=>'省级代理',
				'level'=>110,
			),
			array(
				'id'=>'a2',
				'title'=>'市级代理',
				'level'=>109,
			),
			array(
				'id'=>'a3',
				'title'=>'区级代理',
				'level'=>108,
			),
			array(
				'id'=>'a4',
				'title'=>'镇级代理',
				'level'=>107,
			)
	);
	foreach($list as $k=>&$lv){
		for($i=1;$i<=9;$i++){
			$lv['rate'.$i]=$default_level[$lv['id'].'_rate'.$i]?:'';
		}
		$lv['price']=$default_level[$lv['id'].'_price'];
	}
	$return['list']=$list;
	$params=array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$levels=pdo_getall('jy_weishop_member_level',$params,array(),'',$orderby);
	foreach($levels as &$item){
		$nowlevel=$this->get('level',array('shoplevelid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		if(!$nowlevel['id']){
			$nowlevel=array(
				'title'=>trim($item['title']),
				'level'=>intval($item['level']),
				'shoplevelid'=>$item['id'],
			);
			$nowlevel['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_area_psi_level',$nowlevel);
			$nowlevel['id']=pdo_insertid();
		}
		$return['list'][]=$nowlevel;
	}
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、【直推购物消费奖CV】：下级下单购买，上级享受N倍贡献值；
		</br>2、【直推购物消费奖佣金】：下级下单购买，上级享受佣金；
		</br>3、【直推蛋鸡消费奖CV】：蛋鸡租养、VIP收入比例奖励N倍CV贡献值；
		</br>4、【代理区域购物消费奖佣金】：按订单收货地址，常规区域代理模式；
		</br>5、【小粉丝群购物消费奖CV】：按关系链1，推荐人关系链，但动态区分大小群。；
		</br>6、【小粉丝群蛋鸡消费奖CV】：按关系链1，推荐人关系链，但动态区分大小群。；
		</br>7、【私厨成员蛋鸡消费奖佣金】：私厨成员蛋鸡消费奖励：虚拟商品没有收货地址，所以代理按手动绑定的关系链获得奖励。当有私厨成员支付520元租养蛋鸡时，他所绑定的私厨得30元，私厨所绑定的区代得10元，区代所绑定的市代得5元，市代所绑定的省代得3元。
		</br>8、【私厨成员等级奖励佣金】：：每一个达到VIP等级的会员，他所绑定的私厨都能获得30元。类似 微客分销-邀请奖励 邀请一个VIP等级获得30元。但只有特定等级 私厨 可以邀请获得奖励，其它等级邀请VIP没有奖励。
		','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
			array('name'=>'price','title'=>'开通费用','type'=>'string'),
			array('name'=>'rate1','title'=>'直推购物消费奖CV','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'rate2','title'=>'直推购物消费奖佣金','type'=>'string'),
			array('name'=>'rate3','title'=>'直推蛋鸡消费奖CV','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'rate4','title'=>'代理区域购物消费奖佣金','type'=>'string'),
			array('name'=>'rate5','title'=>'小粉丝群购物消费奖CV','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'rate6','title'=>'小粉丝群蛋鸡消费奖CV','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'rate7','title'=>'私厨成员蛋鸡消费奖佣金','type'=>'string'),
			array('name'=>'rate8','title'=>'私厨成员等级奖励佣金','type'=>'string'),
			array('name'=>'rate9','title'=>'直推蛋鸡消费奖临时CV','type'=>'unitstring','unit'=>'倍'),
		),'action'=>array(
			//array('op'=>'look_condition','title'=>'升级条件','type'=>'modal','show'=>array('level <'=>100)),
			array('op'=>'edit','title'=>'编辑','type'=>'href'),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look_condition'){
	$item=$this->get('level',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('conditions'));
	if(!$item){
		exi('指定等级不存在！','error');
	}
	$item['conditions']=json_decode($item['conditions'],true);
	$return['item']=$item;
	if(!$item['conditions']){
		exi('该等级尚未设置升级条件！','error');
	}
	$htmls=array(
		'type'=>'look',
		'look'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$htmls['look'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=$_RQ['id'];
	if($id){
		if($id=='a0' || $id=='a1' || $id=='a2' || $id=='a3' || $id=='a4'){
			$item['id']=$id;
			$default_level=$this->plugin_setting('default_level');
			for($i=1;$i<=8;$i++){
				$item['rate'.$i]=$default_level[$id.'_rate'.$i]?:'';
			}
			$item['price']=$default_level[$id.'_price'];
			$item['isdemo']=1;
			if($id=='a0'){
				$item['isdemo']=2;
			}
		}else{
			$item=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array('order_price'=>'0');
			$item['goods_ids']=$item['conditions']['goods_ids'];
			$item['goodsnum']=$item['conditions']['goodsnum'];
			$item['isdemo']=0;
		}
	}else{
		$item=array(
			'title'=>'',
			'conditions'=>array('order_price'=>'0'),
		);
		$item['isdemo']=0;
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'等级设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string','show'=>array('isdemo'=>0)),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'填写一百以内的数字，数字越大表示等级越高！不同等级的等级权重不得相等！','show'=>array('isdemo'=>0)),
			array('name'=>'price','title'=>'开通费用','type'=>'string','help'=>'VIP会员才能申请开通；','show'=>array('isdemo'=>array(0,1))),
			array('name'=>'ismerch','title'=>'属于私厨商户','type'=>'switch','help'=>'付费开通本等级后，会自动开通多商户；','show'=>array('isdemo'=>0)),
			array('name'=>'rate1','title'=>'直推购物消费奖CV','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'rate2','title'=>'直推购物消费奖佣金','type'=>'string','help'=>'填写数字表示固定值，添加数字+%表示按百分比计算；'),
			array('name'=>'rate3','title'=>'直推蛋鸡消费奖CV','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'rate4','title'=>'代理区域购物消费奖佣金','type'=>'string','help'=>'填写数字表示固定值，添加数字+%表示按百分比计算；'),
			array('name'=>'rate5','title'=>'小粉丝群购物消费奖CV','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'rate6','title'=>'小粉丝群蛋鸡消费奖CV','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'rate7','title'=>'私厨成员蛋鸡消费奖佣金','type'=>'string','help'=>'填写数字表示固定值，添加数字+%表示按百分比计算；'),
			array('name'=>'rate8','title'=>'私厨成员等级奖励佣金','type'=>'string','help'=>'填写数字表示固定值，添加数字+%表示按百分比计算；'),
			array('name'=>'rate9','title'=>'直推蛋鸡消费奖临时CV','type'=>'unitstring','unit'=>'倍'),
		))),
	);
	if($id>0|| !$id){
		//升级条件表单
		$level_updesc='全满足才升级(开通费用为0的等级才能自动升级！否则是满足条件才能申请)';
		$condition_edit=array(
			'title'=>'升级条件（'.$level_updesc.'）',
			'list'=>array(),
		);
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->model()->current_level_up_types();
		foreach($current_level_up_types as $type){
			if($type=='level_num'){
				$condition_edit['list'][]=array('type'=>'select','name'=>'conditions.level_id','title'=>'选择指定等级','options'=>'level');
			}
			if($type=='buy_goods'){
				$condition_edit['list'][]=array('type'=>'choose_many','title'=>'购买指定商品升级','name'=>'goods_ids','action'=>'jy_weishop/goods.get_goods/many/','choose_title'=>'选择参与的商品','help'=>'（多个商品时，任意其中一个）注意：注意触发升级时机，需核销的商品，只有核销后才会完成！虚拟商品，发货后自动会完成！');
				$condition_edit['list'][]=array('name'=>'goodsnum','title'=>'购买商品的数量','type'=>'unitstring','unit'=>'个');
			}else{
				$condition_edit['list'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
			}
		}
		$htmls['edits'][]=$condition_edit;
	}
	$return['level']=$this->model('model')->get_levels();
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id=$postdata['id'];
	if($id=='a0' || $id=='a1' || $id=='a2' || $id=='a3' || $id=='a4'){
		$default_level=$this->plugin_setting('default_level');
		for($i=1;$i<=9;$i++){
			$default_level[$id.'_rate'.$i]=$postdata['rate'.$i];
		}
		$default_level[$id.'_price']=$postdata['price'];
		$this->plugin_setting('default_level',$default_level);
		exi('设置成功！','','list');
	}else{
		$postdata['conditions']['goods_ids']=$postdata['goods_ids'];
		$postdata['conditions']['goodsnum']=$postdata['goodsnum'];
			$data=array(
				'title'=>trim($postdata['title']),
				//'area_type'=>intval($postdata['area_type']),
				'level'=>intval($postdata['level']),
				'ismerch'=>intval($postdata['ismerch']),
				'conditions'=>$postdata['conditions']?ijson_encode($postdata['conditions']):'',
				'price'=>$postdata['price'],
				'rate1'=>$postdata['rate1'],
				'rate2'=>$postdata['rate2'],
				'rate3'=>$postdata['rate3'],
				'rate4'=>$postdata['rate4'],
				'rate5'=>$postdata['rate5'],
				'rate6'=>$postdata['rate6'],
				'rate7'=>$postdata['rate7'],
				'rate8'=>$postdata['rate8'],
				'rate9'=>$postdata['rate9'],
			);
			if($data['level']<=0 || $data['level']>=100){
				exi($id.'请填写正确的等级权重值，1-99','error');
			}
			if($data['ismerch']){
				$isadd=pdo_get('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'ismerch'=>1));
				if($isadd['id']!=$postdata['id']){
					exi('已经有等级设置了私厨商户！只能设置一个','error');
				}
			}
			$id=intval($postdata['id']);
			if($id){
				$this->update('level',$data,array('id'=>$id));
				$shopevel=array(
					'title'=>trim($data['title']),
					'level'=>intval($data['level']),
					'up_type'=>4,
				);
				pdo_update('jy_weishop_member_level',$shopevel,array('id'=>$postdata['shoplevelid'],'uniacid'=>$_SESSION['uniacid']));
				exi('设置成功！','','list');
			}else{
				$shopevel=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>trim($data['title']),
					'level'=>intval($data['level']),
					'status'=>1,
					'up_type'=>4,
				);
				pdo_insert('jy_weishop_member_level',$shopevel);
				$data['shoplevelid']=pdo_insertid();
				$data['uniacid']=$_SESSION['uniacid'];
				$this->insert('level',$data);
				exi('添加成功！','','list');
			}
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$nowlevel=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$this->delete('level',array('id'=>$id));
		pdo_delete('jy_weishop_member_level',array('id'=>$nowlevel['shoplevelid'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('该等级不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$nowlevel=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				$this->delete('level',array('id'=>$id));
				pdo_delete('jy_weishop_member_level',array('id'=>$nowlevel['shoplevelid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>